package com.sky.service.common.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.common.DishFlavorsMapper;
import com.sky.mapper.common.DishMapper;
import com.sky.mapper.common.SetmealMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.common.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class DishServicempl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;
    @Autowired
    private SetmealMapper setmealDishMapper;

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        //添加菜品
        dishMapper.addDish(dish);
        //根据菜名查询菜品id
        Long dishId = dishMapper.queryDishIdByDishName(dish.getName());
        //添加菜品口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
            dishFlavorsMapper.addDishFlavor(flavors);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        //查询ids中的菜品有没有是起售中的
        Integer count = dishMapper.selectCountByIdsAndStatus(ids, StatusConstant.ENABLE);
        if(count>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }
        //查询ids中的菜品有没有被套餐关联的
        Integer count2= setmealDishMapper.selectCountByDishIds(ids);
        if(count2>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品数据
        boolean b = dishMapper.deleteDishByIds(ids);
        //删除菜品口味数据
        dishFlavorsMapper.deleteDishFlavorsByIds(ids);
    }

    @Override
    public DishVO findDishById(Long dishId) {
        //查询菜品数据根据id
        DishVO dishVO = dishMapper.findDishById(dishId);
        //查询菜品口味数据
        List<DishFlavor> dishFlavors = dishFlavorsMapper.findDishFlavorsById(dishId);
        if (dishFlavors != null && dishFlavors.size() > 0){
            dishVO.setFlavors(dishFlavors);
            log.warn("查询的菜品数据：{}", dishVO);
        }else {
            log.error("根据id查询的菜品口味数据为空");
        }

        return dishVO;
    }

    /**
     * 修改菜品
     * @param dishDTO 修改的菜品数据
     */
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //修改菜品数据
        Integer update = dishMapper.update(dish);
        System.out.println(update);
        //修改菜品口味数据

        //获取前端传过来的菜品口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //判断从前端传过来的菜品口味数组是否为空
        if (flavors != null && flavors.size() > 0) {
            //删除当前菜品的口味数据
            dishFlavorsMapper.deleteDishFlavorsById(dishDTO.getId());

            //遍历菜品口味数据，给菜品口味数据的dishId赋值
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishDTO.getId());
            }
            //删除完成之后，添加新的菜品口味数据
            dishFlavorsMapper.addDishFlavor(flavors);
        }
    }

    @Override
    public List<Dish> findDishByCategoryId(Long categoryId) {
        //构造查询条件
        Dish dish =Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();

        List<Dish> dishList = dishMapper.findDishByCategoryId(dish);
        return dishList;
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.findDishByCategoryId(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorsMapper.findDishFlavorsById(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

}
